package org.lib.jpamysql.oto;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import lombok.Data;

/**
 * 一对一 扩展：和OtoBasic是 一对一关系
 * @author ben
 * @date 2021-10-18 14:03:46 CST
 */
@Entity
@Data
public class OtoExt {

	/**
	 * 自增
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	/**
	 * 详情
	 */
	@Column(columnDefinition = "VARCHAR(100) NOT NULL")
	private String detail;
	
	// 不能有！
//	private Long basicId;

	// 1、无法删除
//	@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE})
	// 2、无法删除 发生异常
//	@OneToOne(cascade = {CascadeType.REFRESH, CascadeType.REMOVE})
	// 3、无法删除 发生异常
//	@OneToOne(cascade = {CascadeType.REMOVE})
	// 在主表使用 CascadeType.REMOVE 才有效：删除主表记录，级联删除从表，，而不是在从表设置
	@OneToOne
	@JoinColumn(name = "basic_id", referencedColumnName = "id", foreignKey = @ForeignKey(name="fk_basic_id", value=ConstraintMode.CONSTRAINT))
	private OtoBasic basic;
	
}
